package com.share.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @desc:
 * @author:caifan
 * @date:2022/3/25
 */
public class ThreadLocalDemo02 {

    private static ExecutorService threadPool = Executors.newFixedThreadPool(16);
    private static ThreadLocal<SimpleDateFormat> formatLocal = new ThreadLocal<SimpleDateFormat>() {

        @Override
        public SimpleDateFormat initialValue() {
            return new SimpleDateFormat("mm:ss");
        }
    };
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");

    public static void main(String[] args) throws InterruptedException {
        ThreadLocalDemo02 threadLocalDemo02 = new ThreadLocalDemo02();
        for (int i = 1; i <= 300; i++) {
            int finalI = i;
            threadPool.submit(() -> {
                String date = threadLocalDemo02.date(finalI);
                System.out.println(date);
            });

        }
        threadPool.shutdown();
    }

    public String date(int seconds) {
        Date date = new Date(1000 * seconds);
        return formatLocal.get().format(date);
//        return simpleDateFormat.format(date);
    }
}
